package help

import (
	"gopkg.in/mgo.v2"
	// "fmt"
	log "github.com/sirupsen/logrus"
	"gopkg.in/mgo.v2/bson"
	"libs/db"
	
)

type Model struct {
	Mongodb *db.Mongodb
	// Redis   *db.Redis
	Table   string
}

type JsonData struct {
	Name            string `bson:"name" json:"name"`
	Language	    string `bson:"language" json:"language"`
	Key             string `bson:"key" json:"key"`
	NewsId          string `bson:"newsid" json:"newsid"`
	Url             string `bson:"url" json:"url"`
	Id              bson.ObjectId `bson:"_id" json:"id"`
}

// func New(c *gin.Context) *Controller{
func NewModel(host string, port int, dbname string, args ...interface{}) *Model{
	model  := new(Model)
	model.Mongodb = db.NewMongodb(host, port, dbname, args...)
	model.Table   = "help"
	return model
}

func (this *Model) GetTable() string{
    return this.Table
}

func (this *Model) SetTable(table string){
    this.Table = table
}

func (this *Model) Insert(body JsonData, lang string) (JsonData, error) {
	var err error
	body.Id = bson.NewObjectId()
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		err = c.Insert(body)
		if err != nil {
			log.Errorf("[%s]Insert error, [Name: %s]", this.GetTable(), body.Name)
		}else{
			log.Debugf("[%s]Insert success, [Name: %s]", this.GetTable(), body.Name)
		}
    })
	return body, err
}

func (this *Model) Delete(id string, lang string) (string, error) {
	var err error
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		if err = c.Remove(bson.M{"_id": bson.ObjectIdHex(id)}); err != nil {
			if err.Error() != mgo.ErrNotFound.Error() {
				log.Errorf("[%s]Delete error, [id: %s]", this.GetTable(), id)
			}else{
				log.Debugf("[%s]Delete ErrNotFound, [id: %s]", this.GetTable(), id)
				err = mgo.ErrNotFound
			}
		}
    })
	return id, err
}

func (this *Model) Update(id string, body JsonData, lang string) (JsonData, error) {
	var err error
	body.Id = bson.ObjectIdHex(id)
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		if err = c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, body); err != nil {
			if err.Error() != mgo.ErrNotFound.Error() {
				log.Errorf("[%s]Update error, [id: %s]", this.GetTable(), id)
			}else{
				log.Debugf("[%s]Update ErrNotFound, [id: %s]", this.GetTable(), id)
				err = mgo.ErrNotFound
			}
		}
    })
	return body, err
}

func (this *Model) FindAll(skip int ,limit int, lang string) ([]JsonData, error) {
	var err error
	var coin []JsonData
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		if err = c.Find(nil).Sort("order").Limit(limit).Skip(skip).All(&coin); err != nil {
			if err.Error() != mgo.ErrNotFound.Error() {
				log.Errorf("[%s]FindAll error, [skip: %d, limit: %d]", this.GetTable(), skip, limit)
			}else{
				log.Debugf("[%s]FindAll ErrNotFound, [skip: %d, limit: %d]", this.GetTable(), skip, limit)
				err = mgo.ErrNotFound
			}
		}
    })
	return coin, err
}

func (this *Model) Count(lang string) (int, error) {
	var err error
	var length int
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		if length,err = c.Find(nil).Count(); err != nil {
			if err.Error() != mgo.ErrNotFound.Error() {
				log.Errorf("[%s]Count error, [language: %s]", this.GetTable(), lang)
			}else{
				log.Debugf("[%s]Count ErrNotFound, [language: %s]", this.GetTable(), lang)
				err = mgo.ErrNotFound
			}
		}
    })
	return length, err
}

func (this *Model) FindOne(id string, lang string) (JsonData, error) {
	var err error
	var coin JsonData
	this.Mongodb.M(lang, func(c *mgo.Collection) {
		if err = c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&coin); err != nil {
			if err.Error() != mgo.ErrNotFound.Error() {
				log.Errorf("[%s]FindOne error, [id: %s, lang: %s]", this.GetTable(), id, lang)
			}else{
				log.Debugf("[%s]FindOne ErrNotFound, [id: %s, lang: %s]", this.GetTable(), id, lang)
				err = mgo.ErrNotFound
			}
		}
    })
	return coin, err
}
